#include <REG52.H>
#include "Delay.h"
#define KeyPort P3
/*------------------------------------------------
          按键扫描函数，返回扫描键值
------------------------------------------------*/
unsigned char KeyScan(void)  //键盘扫描函数，使用行列反转扫描法
{
 unsigned char H,L;//行列值中间变量
 KeyPort=0x0f;            //行线输出全为0
 H=KeyPort&0x0f;     //读入列线值
 if(H!=0x0f)    //先检测有无按键按下
 {
  DelayMs(10);        //去抖
  if((KeyPort&0x0f)!=0x0f)
  {
    H=KeyPort&0x0f;  //读入列线值
    KeyPort=H|0xf0;  //输出当前列线值
    L=KeyPort&0xf0;  //读入行线值

    while((KeyPort&0xf0)!=0xf0);//等待松开并输出

    return(H+L);//键盘最后组合码值
   }
  }return(0xff);     //返回该值
}
/*------------------------------------------------
              按键值处理函数，返回扫键值
------------------------------------------------*/
unsigned char KeyPro(void)
{
 switch(KeyScan())
 {
  case 0x7e:return 1;break;//1 按下相应的键显示相对应的码值
  case 0x7d:return 2;break;//2
  case 0x7b:return 3;break;//3
  case 0x77:return 4;break;//4
  case 0xbe:return 5;break;//5
  case 0xbd:return 6;break;//6
  case 0xbb:return 7;break;//7
  case 0xb7:return 8;break;//8
  case 0xde:return 9;break;//9
  case 0xdd:return 10;break;//10
  case 0xdb:return 11;break;//11
  case 0xd7:return 12;break;//12
  case 0xee:return 13;break;//13
  case 0xed:return 14;break;//14
  case 0xeb:return 15;break;//15
  case 0xe7:return 16;break;//16
  default:return 0xff;break;
 }
}
